package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 常肖飞先生
 * @version V1.0
 * @Project day41_1
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/11 9:29
 */
@WebServlet(name = "LifeServlet",urlPatterns = "/life")
public class LifeServlet extends HttpServlet {

    private int value;

    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("LifeServlet.init");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        response.setContentType("text/html;charset=utf-8");

//
        synchronized (this){//单例类，可以使用this。
            System.out.println(this.hashCode());
            String method = request.getParameter("method");
            if("add".equals(method)){
                value ++;
            }else if("minus".equals(method)){
                value --;
            }
            response.getWriter().println("当前值 = " + value);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


    @Override
    public void destroy() {
        super.destroy();
        System.out.println("LifeServlet.destroy");
    }
}
